cap log close

global path = "****"

local name= "monthly_cov_effect"
local date = "051820"
global logname = "$path/`name'_`date'.log"

clear
set more off
cd "$path"

log using "$logname", replace

use "$path/Data/penalty_sample.dta", clear

cap drop cov_type

*****************************
*Outcome = any coverage (cov_type=8)
*****************************
gen cov_type=8 
save temp,replace

*Merge in monthly coverage information
forval year=2016/2018 {
	use  "$path/Data/monthly_cov_`year'" ,clear
	keep if cov_type==8
	duplicates drop cov_type dep_tin tin tin_typ tax_prd extcycle ,force
	destring month*,replace
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp,keep(2 3)

	forval i=1/12 {
		**destring month`i', replace
		replace month`i'=0 if _merge==2
		if `year'>2016{
		local j=`i'+12*(`year'-2016)
		rename month`i' month`j'
		}
	}
drop _merge
	if `year' ==2016{
	save temp1,replace
	}
	if `year'>2016{
	keep cov_type dep_tin tin tin_typ tax_prd extcycle month*
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp1
	drop _merge
	save temp1,replace
	}
}


*export means to datasets
*create command line:
local cmd ""
forval i=1/36 {
	local cmd " `cmd' (mean) month`i' (semean) month`i'_se=month`i'"
}


*full sample
preserve
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_cov_full", replace
restore
*not all 16 coverage
preserve
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_cov_notall", replace
restore
*not all 16, 45-64
preserve
keep if age_2017>=45 & age_2017<65
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_cov_notall_4564", replace
restore


estimates clear
eststo clear

cap erase "$path/monthly_cov_effect_`date'.ster"
cap erase "$path/monthly_cov_effect_4564_`date'.ster"

forval i=1/36{

rename month`i' any_coverage_month_`i'

reg any_coverage_month_`i' treatment if  notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_cov_effect_`date'.ster", append

reg any_coverage_month_`i' treatment if  notall16 == 1 & age_2017 < 65 & age_2017 >= 45, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_cov_effect_4564_`date'.ster", append

}

*****************************
*Outcome = exchange (cov_type=2)
*****************************
cap drop cov_type
cap drop month*
gen cov_type=2 
use temp,clear
replace cov_type=2
save temp,replace

*Merge in monthly coverage information

forval year=2016/2018 {
	use  "$path/Data/monthly_cov_`year'" ,clear
	keep if cov_type==2
	duplicates drop cov_type dep_tin tin tin_typ tax_prd extcycle ,force
	destring month*,replace
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp,keep(2 3)

	forval i=1/12 {
		**destring month`i', replace
		replace month`i'=0 if _merge==2
		if `year'>2016{
		local j=`i'+12*(`year'-2016)
		rename month`i' month`j'
		}
	}
drop _merge
	if `year' ==2016{
	save temp1,replace
	}
	if `year'>2016{
	keep cov_type dep_tin tin tin_typ tax_prd extcycle month*
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp1
	drop _merge
	save temp1,replace
	}
}


*export means to datasets
*create command line:
local cmd ""
forval i=1/36 {
	local cmd " `cmd' (mean) month`i' (semean) month`i'_se=month`i'"
}


*full sample
preserve
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_exch_full", replace
restore
*not all 16 coverage
preserve
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_exch_notall", replace
restore
*not all 16, 45-64
preserve
keep if age_2017>=45 & age_2017<65
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_exch_notall_4564", replace
restore



estimates clear
eststo clear

cap erase "$path/monthly_exchange_effect_`date'.ster"
cap erase "$path/monthly_exchange_effect_4564_`date'.ster"

forval i=1/36 {

rename month`i' any_exchange_month_`i'

reg any_exchange_month_`i' treatment if  notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_exchange_effect_`date'.ster", append

reg any_exchange_month_`i' treatment if  notall16 == 1 & age_2017 < 65 & age_2017 >= 45, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_exchange_effect_4564_`date'.ster", append
}



*****************************
*Outcome = medicaid (cov_type=5)
*****************************
cap drop cov_type
cap drop month*
gen cov_type=5 
use temp,clear
replace cov_type=5
save temp,replace

*Merge in monthly coverage information

forval year=2016/2018 {
	use  "$path/Data/monthly_cov_`year'" ,clear
	keep if cov_type==5
	duplicates drop cov_type dep_tin tin tin_typ tax_prd extcycle ,force
	destring month*,replace
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp,keep(2 3)

	forval i=1/12 {
		**destring month`i', replace
		replace month`i'=0 if _merge==2
		if `year'>2016{
		local j=`i'+12*(`year'-2016)
		rename month`i' month`j'
		}
	}
drop _merge
	if `year' ==2016{
	save temp1,replace
	}
	if `year'>2016{
	keep cov_type dep_tin tin tin_typ tax_prd extcycle month*
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp1
	drop _merge
	save temp1,replace
	}
}


*export means to datasets
*create command line:
local cmd ""
forval i=1/36 {
	local cmd " `cmd' (mean) month`i' (semean) month`i'_se=month`i'"
}


*full sample
preserve
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_med_full", replace
restore
*not all 16 coverage
preserve
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_med_notall", replace
restore
*not all 16, 45-64
preserve
keep if age_2017>=45 & age_2017<65
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_med_notall_4564", replace
restore


estimates clear
eststo clear

cap erase "$path/monthly_medicaid_effect_`date'.ster"
cap erase "$path/monthly_medicaid_effect_4564_`date'.ster"


forval i=1/36 {

rename month`i' any_medicaid_month_`i'

reg any_medicaid_month_`i' treatment if  notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_medicaid_effect_`date'.ster", append

reg any_medicaid_month_`i' treatment if  notall16 == 1 & age_2017 < 65 & age_2017 >= 45, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_medicaid_effect_4564_`date'.ster", append
}

*/

*****************************
*Outcome = ESI (cov_type=1)
*****************************

cap drop cov_type
cap drop month*
gen cov_type=1 
use temp,clear
replace cov_type=1
save temp,replace

*Merge in monthly coverage information

forval year=2016/2018 {
	use  "$path/Data/monthly_cov_`year'" ,clear
	keep if cov_type==1
	duplicates drop cov_type dep_tin tin tin_typ tax_prd extcycle ,force
	destring month*,replace
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp,keep(2 3)

	forval i=1/12 {
		**destring month`i', replace
		replace month`i'=0 if _merge==2
		if `year'>2016{
		local j=`i'+12*(`year'-2016)
		rename month`i' month`j'
		}
	}
drop _merge
	if `year' ==2016{
	save temp1,replace
	}
	if `year'>2016{
	keep cov_type dep_tin tin tin_typ tax_prd extcycle month*
	merge 1:1 cov_type dep_tin tin tin_typ tax_prd extcycle using temp1
	drop _merge
	save temp1,replace
	}
}


*export means to datasets
*create command line:
local cmd ""
forval i=1/36 {
	local cmd " `cmd' (mean) month`i' (semean) month`i'_se=month`i'"
}


*full sample
preserve
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_esi_full", replace
restore
*not all 16 coverage
preserve
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_esi_notall", replace
restore
*not all 16, 45-64
preserve
keep if age_2017>=45 & age_2017<65
keep if covered2016-any_dec<11 
collapse `cmd' (count) N=month1, by(treatment)
save "$path/monthly_esi_notall_4564", replace
restore


estimates clear
eststo clear

cap erase "$path/monthly_esi_effect_`date'.ster"
cap erase "$path/monthly_esi_effect_4564_`date'.ster"


forval i=1/36 {

rename month`i' any_esi_month_`i'

reg any_esi_month_`i' treatment if  notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_esi_effect_`date'.ster", append

reg any_esi_month_`i' treatment if  notall16 == 1 & age_2017 < 65 & age_2017 >= 45, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)& treatment==0
estadd scalar ymean=r(mean)*100
estimates save "$path/monthly_esi_effect_4564_`date'.ster", append
}



log close






